// pages/forward/forward.js
const app = getApp()
const url = app.basicsUrl();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        gameId: '',
        hongBaoHardType: '',
        hongbaoId: '',
        msg: '',
        hasPickMoney: '',
        money: '',
        hongbaoData: "",
        name: "",
        info: "",
        userName: "",
        left: "",
        shareImg: ""
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        var hongbaoId = options.hongbaoId;
        var msg = options.msg;
        var hasPickMoney = options.hasPickMoney;
        var money = options.money;
        that.setData({
            hongbaoId: hongbaoId,
            msg: msg,
            hasPickMoney: hasPickMoney,
            money: money
        })
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    winWidth: res.windowWidth,
                    winHeight: res.windowHeight
                });
            }
        });
    },

    /**
     * 点击跳转玩游戏界面
     */
    bindplay: function () {
        var that = this;
        var gameId = that.data.gameId;
        var hongBaoHardType = that.data.hongBaoHardType;
        var hongbaoId = that.data.hongbaoId;
        wx.redirectTo({
            url: '/pages/games/games?gameId=' + gameId + '&hongBaoHardType=' + hongBaoHardType + '&hongBaoId=' + hongbaoId + '&left=' + 1
        })
    },

    /**
     * 点击生成朋友圈分享图
     */
    bindphoto: function () {
        var preImgurl;
        var that = this;
        var gameId = that.data.gameId;//游戏id
        var hongBaoHardType = that.data.hongBaoHardType;//游戏困难度
        var title = that.data.name;//游戏名称
        var content = that.data.info;//红包描述
        var path = "pages%2fgames%2fgames";
        var memberName = that.data.userName;//用户的姓名
        wx.getStorage({
            key: 'loginInfo',
            success: function (res) {
                var data = res.data;
                var openId = data.openId;
                var tokenKey = data.id;
                var token = data.token;
                preImgurl = encodeURI(url + 'authorized/shareImg?path=' + path + '&tokenKey=' + tokenKey + '&name=' + memberName + '&title=' + title + '&content=' + content + '&openId=' + openId + '&token=' + token);
                wx.previewImage({
                    current: preImgurl, // 当前显示图片的http链接
                    urls: [preImgurl] // 需要预览的图片http链接列表
                })

            }
        })


    },
    onReady: function () {

    },
    onShow: function () {
        this.init();
    },
    onHide: function () {

    },
    onUnload: function () {

    },
    onPullDownRefresh: function () {

    },
    onReachBottom: function () {

    },
    createCanvas(headImg) {
        var that = this;
        var context = wx.createCanvasContext('firstCanvas')
        context.drawImage('../../images/share.jpg', 0, 0, 350, 350)
        wx.downloadFile({
            url: headImg,
            success: function (res) {
                context.drawImage(res.tempFilePath, 7, 22, 40, 40)
                context.drawImage(res.tempFilePath, 7, 116, 40, 40)
                context.drawImage(res.tempFilePath, 7, 166, 40, 40)
                context.draw()
            }
        })

        wx.showLoading({
            title: '加载中...',
        })
        setTimeout(function () {
            wx.hideLoading()
            wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: 350,
                height: 350,
                destWidth: 350,
                destHeight: 350,
                canvasId: 'firstCanvas',
                success: function (res) {
                    var shareImg = res.tempFilePath;
                    that.setData({
                        shareImg: shareImg
                    })
                }
            })
        }, 2000)
    },
    onShareAppMessage: function (res) {
        var that = this;
        var shareImg = that.data.shareImg;
        var gameId = that.data.gameId;
        var hongBaoHardType = that.data.hongBaoHardType;
        var hongbaoId = that.data.hongbaoId;
        if (res.from === 'button') {
        }
        return {
            title: '给你发了大红包，赶紧领！',
            path: '/pages/games/games?gameId=' + gameId + '&hongBaoHardType=' + hongBaoHardType + '&hongBaoId=' + hongbaoId + '&left=' + that.data.left,
            imageUrl: shareImg,
            success: function (res) {
            },
            fail: function (res) {
            }
        }
    },
    init() {
        var that = this;
        wx.getStorage({
            key: 'loginInfo',
            success: function (res) {
                var params = {};
                params['hongBaoId'] = that.data.hongbaoId;
                params['tokenKey'] = res.data.id;//红包描述信息
                params['openId'] = res.data.openId;//红包描述信息
                params['token'] = res.data.token;//红包描述信息
                wx.request({
                    url: url + "authorized/hongBao",
                    data: params,
                    header: {
                        'content-type': 'application/json'
                    },
                    success: res => {
                        var data = res.data;
                        if (data.code == 200) {
                            data.data.name = decodeURI(data.data.name);
                            var hongbaoData = data.data;
                            var hongBaoInfo = hongbaoData.hongBao;
                            var gameId = hongBaoInfo.gameId;
                            var hongBaoHardType = hongBaoInfo.hongBaoHardType;
                            var gameInfo = hongbaoData.game;
                            that.createCanvas(hongbaoData.headImg)
                            that.setData({
                                hongbaoData: hongbaoData,
                                gameId: gameId,
                                hongBaoHardType: hongBaoHardType,
                                name: gameInfo.name,
                                info: gameInfo.info,
                                userName: hongbaoData.name,
                                left: hongbaoData.left
                            })
                        }
                    }
                })
            },
        })
    }
})